package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/13 15:13
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
 */
public class Title714 {

    /**
     * 状态 0：不操作、1：买、2：卖
     * dp[i][j]：表示第i天保持状态j时有多少钱
     * 递推公式：
     *        dp[i][1] = Math.max(dp[i-1][0] - prices[i], dp[i-1][1], dp[i-1][2] - prices[i])
     *        dp[i][2] = Math.max(dp[i-1][2], dp[i-1][1] + prices[i] - fee)
     * @param prices
     * @param fee
     * @return
     */
    public int maxProfit(int[] prices, int fee) {
        int[][] dp = new int[prices.length][3];
        dp[0][1] = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2] - prices[i]);
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i] - fee);
        }
        return dp[prices.length - 1][2];
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,0,2};

    }
}
